package com.lrkj.back.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util. List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lrkj.back.StartUpOnLoadData;
import com.lrkj.back.entity.ZsDabao;
import com.lrkj.back.entity.ZsPackageImformation;
import com.lrkj.back.entity.ZsWupinBaoFafang;
import com.lrkj.back.vo.HuishouZhuangtai;
import com.lrkj.back.Const;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lrkj.back.entity.TbDepartment;
import com.lrkj.back.entity.TbEmployee;
import com.lrkj.back.entity.TbPackageMuban;
import com.lrkj.back.entity.ZsHuishou;
import com.lrkj.back.entity.ZsHuishouMingxi;
import com.lrkj.back.entity.ZsWupinFafang;
import com.lrkj.back.entity.ZsWupinYcpFafang;
import com.lrkj.back.service.TbDepartmentService;
import com.lrkj.back.service.TbEmployeeService;
import com.lrkj.back.service.TbPackageMubanService;
import com.lrkj.back.service.TbSeqService;
import com.lrkj.back.service.ZsHuishouMingxiService;
import com.lrkj.back.service.ZsHuishouService;
import com.lrkj.back.service.ZsPackageImformationService;
import com.lrkj.back.service.ZsShenqingShoushubaoMingxiService;
import com.lrkj.back.service.ZsShenqingYicipinMingxiService;
import com.lrkj.back.service.ZsWupinBaoFafangService;
import com.lrkj.back.service.ZsWupinFafangService;
import com.lrkj.back.service.ZsWupinYcpFafangService;
import com.lrkj.back.vo.KucunNum;
import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/wupingfafang")
public class ZsWupingfafangController extends BaseController {
    
	@Autowired
	private ZsShenqingYicipinMingxiService sqmxService;
	@Autowired
	private TbSeqService seqService;
	@Autowired
	private ZsShenqingShoushubaoMingxiService ssbservice;
	@Autowired
	private TbDepartmentService departmentservice;
	@Autowired 
	private TbPackageMubanService mubanservice;
	@Autowired
	private ZsWupinFafangService fafangservice;
	@Autowired
	private ZsWupinYcpFafangService ycpffservice;
	@Autowired
	private ZsWupinBaoFafangService baofafangservice;
	@Autowired
	private ZsPackageImformationService packageInfomationservice;
	@Autowired
	private ZsWupinFafangService wupingfafangservice;
	@Autowired
	private TbDepartmentService tbDepartmentService;
	@Autowired
	private TbPackageMubanService packagemubanService;
	@Autowired
	private TbEmployeeService employeeService;
	@Autowired
	private ZsHuishouMingxiService huishouMingxiService;
	@Autowired
	private ZsHuishouService huishouService;

	//物品发放--物品发放记录  （物品发放记录点击查询）
	@RequestMapping("/fafangList.do")
	public ModelAndView yicipinApplyList(String searchTxt,
			HttpServletRequest request, HttpServletResponse response) throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex") == null ? 1 : Integer.valueOf(request.getParameter("pageIndex"));
		Map<String, Object> map = new HashMap<>();
		if (StringUtils.isNotBlank(searchTxt)) {
			searchTxt = searchTxt.trim();
			int fafangkeshi=Integer.valueOf(searchTxt);
			map.put("fangfaKeshi", fafangkeshi);
		}
		// 查所有信息
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);

		List<TbDepartment> alldeparts=departmentservice.findAllDepartment();
		request.setAttribute("alldeparts",alldeparts );
		// 把分页信息传过去
		PageInfo<ZsWupinFafang> page = fafangservice.findAllfafangwuping(map);
		request.setAttribute("fafangList", page.getList());
		request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
		request.setAttribute("total", page.getPages());
		request.setAttribute("empmap", StartUpOnLoadData.emMap);
		return new ModelAndView("/wupinfafang/wupinfangfajilu/fafang_list");
	}

	// 右边显示详细信息的方法,这个是手术包发放用的
	@RequestMapping("/shoushubaofafangDetailList.do")
	@ResponseBody
	public Map<Object, Object> shoushubaoApplyDetailList(String id,
			HttpServletRequest request, HttpServletResponse response) throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {
			Map<String, Object> map = new HashMap<>();
			if (StringUtils.isNotBlank(id)) {
				map.put("fafangLiushui", id);
			}
			List<ZsWupinBaoFafang> mingxiList = baofafangservice.findAllWupinbao(map);
			rtnMap.put("mxList", mingxiList);
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}

	// 右边显示详细信息的方法,这个是一次品用的
	@RequestMapping("/yicipinfafangDetailList.do")
	@ResponseBody
	public Map<Object, Object> yicipinApplyDetailList(String id, HttpServletRequest request,
			HttpServletResponse response) throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object, Object>();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			if (StringUtils.isNotBlank(id)) {
				map.put("fafangLiushui", id);
			}
			List<ZsWupinYcpFafang> mingxiList = ycpffservice.findAllyicipin(map);
			rtnMap.put("mxList", mingxiList);
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}

	// 物品发放--手术室包发放   点击发放
	@RequestMapping("/shoushushibaoList.do")
	public ModelAndView packageingList(String searchTxt,
			HttpServletRequest request, HttpServletResponse response) throws BusinessException{
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> mubanmap = new HashMap<String,Object>();
		List<String> packageIdList = new ArrayList<String>();
	//	mubanmap.put("packageDepartid", 15);
		List<TbPackageMuban> mubanList = mubanservice.findAllPackageNoPageList(mubanmap);
		for (TbPackageMuban tbPackageMuban : mubanList) {
			if (tbPackageMuban.getPackageDepartid() == 15 || tbPackageMuban.getPackageDepartid() == 17) {
				if (!packageIdList.contains(tbPackageMuban.getPackageId())) {
					packageIdList.add(String.valueOf(tbPackageMuban.getPackageId()));
				}
			}
		}
		if (packageIdList.size()!=0) {
			//map.put("packageIdArr", packageIdList);
			map.put("miejunHege", 3);
			map.put("miejunZhuangtai", 2);
			map.put("shoushu", "shoushu");
			map.put("miejunshehetime", "miejunshehetime");
			List<ZsPackageImformation> packageImformations = packageInfomationservice.findAll(map);
			request.setAttribute("shoushubaoList", packageImformations);
		}else {
			request.setAttribute("shoushubaoList", "");
		}
		List<TbDepartment> alldeparts=departmentservice.findAllDepartment();
		request.setAttribute("alldeparts",alldeparts );
		request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
		return new ModelAndView("/wupinfafang/shoushushibaofafang/shoushushibaofafang_list");
	}

	// 手术室包发放   点击发放
	@RequestMapping("/piliangfafang.do")
	@ResponseBody
	public Map<Object, Object> piliangfafang(String fafangren,String [] packageTiaoma) throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {
			Date date = new Date();
			String [] baotiaomas = null;
			if(packageTiaoma !=null){
				baotiaomas = packageTiaoma;
			}else{
				baotiaomas = request.getParameter("baotiaomas").split(",");
			}

			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String proId = sdf.format(date)+ seqService.seqVal("wupinfafangno");
			TbEmployee tbEmployee = employeeService.selectByEmployeeNo(fafangren);
			fafangren = fafangren.toUpperCase();
			if (tbEmployee!=null) {
				if (baotiaomas.length > 0) {
					ZsWupinFafang wupingfafang = new ZsWupinFafang();
					wupingfafang.setFafangLiushui(proId);
					wupingfafang.setFangfangLeixing(0);
					wupingfafang.setFafangRiqi(date);
		            wupingfafang.setFangfaKeshi(15);
					wupingfafang.setRecordTime(date);
                    wupingfafang.setFafangrenTiaoma(fafangren.toUpperCase());
					wupingfafangservice.insert(wupingfafang);
					List<ZsWupinBaoFafang> wupingbaos = new ArrayList<>();
					List<ZsPackageImformation> packgaesListOne = new ArrayList<>();
					Long[] seqArr = seqService.seqValBatch("baofafangliushui",baotiaomas.length);
					// 发放明细表中插入所有数据
					for (int i = 0; i < baotiaomas.length; i++) {
						ZsWupinBaoFafang baofafang = new ZsWupinBaoFafang();
						baofafang.setRecordTime(date);
						// 主键
						//FIXME 发放时包的明细表中修改数据不成功时，可以申请表中数据已经更改。事务控制失败
						baofafang.setBaoFafangLiushui(sdf.format(date)+ seqArr[i]);
						baofafang.setFafangLiushui(wupingfafang.getFafangLiushui());
						baofafang.setWupinbaoTiaoma(baotiaomas[i]);
						baofafang.setWupinbaoShuliang(1);
						baofafang.setFafangrenTiaoma(fafangren);
						wupingbaos.add(baofafang);
						// baofafangservice.insert(baofafang);
						ZsPackageImformation onePackageImformation = packageInfomationservice.selectByPrimaryKey(baotiaomas[i]);
						onePackageImformation.setFafangLiushui(proId);
						onePackageImformation.setPackageLiuchengZhuangtai(6);
						onePackageImformation.setFafangrenTiaoma(fafangren);
						onePackageImformation.setFafangTime(date);
						onePackageImformation.setPackageLiuchengZhuangtai(6);
						packgaesListOne.add(onePackageImformation);
					}
					//批量插入包发放信息，批量修改packageInfomation表中的发放流水
					baofafangservice.insertPiliang(wupingbaos);
					packageInfomationservice.updateByBatch(packgaesListOne);
					rtnMap.put("status", "true");
					rtnMap.put("msg", "发放成功");
				}
			}else {
				rtnMap.put("status", "false");
				rtnMap.put("msg", "无效发放人条码");
			}
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}

	// 发放记录中点删除记录用到的方法
	@RequestMapping("/packageingDel.do")
	@ResponseBody
	public Map<Object, Object> packageingDel(HttpServletRequest request,
			HttpServletResponse response) throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object, Object>();
//		Map<String, Object> fafangliushuis = new HashMap<String, Object>();
		List<String> suoyoutiaomaList=new ArrayList<String>();
		try {
			//得到所有的发放流水
			String ids = request.getParameter("id");
			String[] arraId = ids.split(",");
			for (int i=0;i<arraId.length;i++) {
				suoyoutiaomaList.add(arraId[i]);
				ZsWupinFafang fafangjiluFafang=wupingfafangservice.selectByPrimaryKey(arraId[i]);
				/*if(fafangjiluFafang!=null){*/
				if(fafangjiluFafang.getFangfangLeixing()==0){
					baofafangservice.deleteByFfls(fafangjiluFafang.getFafangLiushui());
				}else{
					ycpffservice.deleteByffls(fafangjiluFafang.getFafangLiushui());
				}
			}
			//发放总表中先批量删除
			wupingfafangservice.batchDelete(suoyoutiaomaList);
			rtnMap.put("status", "true");
			rtnMap.put("msg", "删除成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}

	//物品发放--临床科室包发放
	@RequestMapping("/linchuangbaoList.do")
	public ModelAndView linchuangbaoList(String searchTxt, HttpServletRequest request, HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex") == null?1:Integer.valueOf(request.getParameter("pageIndex"));
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> mubanMap = new HashMap<>();
		List<TbPackageMuban> mubanList = mubanservice.findAllPackageNoPageList(mubanMap);
		List<String> packageId = new ArrayList<>();
		List<TbDepartment> alldeparts=departmentservice.findAllDepartment();
		request.setAttribute("alldeparts",alldeparts );
		for (TbPackageMuban tbPackageMuban : mubanList) {
			if (tbPackageMuban.getPackageDepartid()!=null) {
				if (tbPackageMuban.getPackageDepartid()!=15 && tbPackageMuban.getPackageDepartid() != 17){
				    packageId.add(tbPackageMuban.getPackageId());
				}
			}
		}
		if (packageId.size()!=0) {
			map.put("packageIdArr", packageId);
			map.put("pageNo", pageNo);
			map.put("pageSize", Const.PAGE_SIZE);
			map.put("miejunHege", 3);
			map.put("miejunZhuangtai", 2);
			map.put("linchuang", "linchuang");
			map.put("miejunshehetime", "miejunshehetime");
//			PageInfo<ZsPackageImformation> zsPackageList =  packageInfomationservice.findAllInformation(map);

			List<ZsPackageImformation> list = packageInfomationservice.findAll(map);

			if(list !=null){
//				request.setAttribute("total", zsPackageList.getPages());
//				request.setAttribute("shoushubaoList", zsPackageList.getList());
//				request.setAttribute("total", zsPackageList.getPages());
				request.setAttribute("shoushubaoList", list);
				request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
			}else {
				request.setAttribute("shoushubaoList", "");
			}
		}else {
			request.setAttribute("total",0);
			request.setAttribute("shoushubaoList", "");
		}
		return new ModelAndView("/wupinfafang/linchuangkeshibao/linchuangbao_list");
	}

	//物品发放--临床科室包发放   点击查询
	@RequestMapping("/chaxun.do")
	public ModelAndView chaxun1(String searchTxt,String keshi,
			HttpServletRequest request, HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex") == null?1:Integer.valueOf(request.getParameter("pageIndex"));
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> keshiMap = new HashMap<>();
		Map<String, Object> mubanMap = new HashMap<>();
		Map<String, Object> nullMap = new HashMap<>();
		String danfeng = request.getParameter("danfeng");
		String tongyong = request.getParameter("tongyong");
		if (!StringUtils.isBlank(danfeng)) {
			request.setAttribute("danfeng", danfeng);
		}
		if (!StringUtils.isBlank(tongyong)) {
			request.setAttribute("tongyong", tongyong);
		}
		List<ZsPackageImformation> list2 = new ArrayList<>();
		TbDepartment department = new TbDepartment();
		Integer keshihao = 0;// 不选科室时
		if (!StringUtils.isBlank(keshi)) {
			department = departmentservice.selectByPrimaryKey(keshi);
		}
		request.setAttribute("department", department);
		List<String> packageIdnull = new ArrayList<>();
		if (keshihao!=0) {
			keshiMap.put("packageDepartid", keshihao);
			List<TbPackageMuban> list = mubanservice.findAllPackageNoPageList(keshiMap);
			if (list.size()!=0) {
				for (TbPackageMuban tbPackageMuban : list) {
					if(!packageIdnull.contains(tbPackageMuban.getPackageId())){
						packageIdnull.add(tbPackageMuban.getPackageId());
					}
				}
			}else {
				packageIdnull.add("0");
			}
			if (packageIdnull.size()!=0) {
				nullMap.put("packageIdArr", packageIdnull);
				list2 = packageInfomationservice.findAll(nullMap);
				if (list2.size()==0) {
					list2.add(new ZsPackageImformation());
				}
			}
		}
 		if (keshihao==0) {
			/*keshiMap.put("packageDepartid", danfeng);*/
 			mubanMap.put("quanbu", "quanbu");
			List<TbPackageMuban> list = mubanservice.findAllPackageNoPageList(keshiMap);
			for (TbPackageMuban tbPackageMuban : list) {
				if(!packageIdnull.contains(tbPackageMuban.getPackageId())){
					packageIdnull.add(tbPackageMuban.getPackageId());
				}
			}
			if (packageIdnull.size()!=0) {
				nullMap.put("packageIdArr", packageIdnull);
				list2 = packageInfomationservice.findAll(nullMap);
			}
		}
		if (list2.size()!=0||keshihao==0) {
//			//选中两个============================================================
			mubanMap.put("quanbu", "quanbu");//不显示科室号为null和15
			if (!StringUtils.isBlank(danfeng)&&!StringUtils.isBlank(tongyong)) {
				//没选科室
				if (keshihao==0) {
					List<TbPackageMuban> mubanList = mubanservice.findAllPackageNoPageList(mubanMap);
					List<String> packageId = new ArrayList<String>();
					if (mubanList.size()!=0) {
						for (TbPackageMuban tbPackageMuban : mubanList) {
							packageId.add(tbPackageMuban.getPackageId());
						}
						if (packageId.size()!=0) {
							map.put("packageIdArr", packageId);
						}
						else {
							map.put("packageIdArr", 0);
						}
					}else {
						map.put("packageIdArr", 0);
					}
				}
				//选了科室
				else {
					mubanMap.put("packageDepartid", keshihao);
					List<TbPackageMuban> mubanList = mubanservice.findAllPackageNoPageList(mubanMap);
					List<String> packageId = new ArrayList<String>();
					if (mubanList.size()!=0) {
						for (TbPackageMuban tbPackageMuban : mubanList) {
							packageId.add(tbPackageMuban.getPackageId());
						}
						if (packageId.size()!=0) {
							map.put("packageIdArr", packageId);
						}
						else {
							map.put("packageIdArr", 0);
						}
					}else {
						map.put("packageIdArr", 0);
					}
				}
			}
			//都没选中============================================================
			if (StringUtils.isBlank(danfeng)&&StringUtils.isBlank(tongyong)) {
				//没选科室
				if (keshihao==0) {
					List<TbPackageMuban> mubanList = mubanservice.findAllPackageNoPageList(mubanMap);
					List<String> packageId = new ArrayList<String>();
					if (mubanList.size()!=0) {
						for (TbPackageMuban tbPackageMuban : mubanList) {
							packageId.add(tbPackageMuban.getPackageId());
						}
						if (packageId.size()!=0) {
							map.put("packageIdArr", packageId);
						}
						else {
							map.put("packageIdArr", 0);
						}
					}else {
						map.put("packageIdArr", 0);
					}
				}
				//选了科室
				else {
					mubanMap.put("packageDepartid", keshihao);
					List<TbPackageMuban> mubanList = mubanservice.findAllPackageNoPageList(mubanMap);
					List<String> packageId = new ArrayList<String>();
					if (mubanList.size()!=0) {
						for (TbPackageMuban tbPackageMuban : mubanList) {
							packageId.add(tbPackageMuban.getPackageId());
						}
						if (packageId.size()!=0) {
							map.put("packageIdArr", packageId);
						}
						else {
							map.put("packageIdArr", 0);
						}
					}else {
						map.put("packageIdArr", 0);
					}
				}
			}
			//选中第一个============================================================
			if (!StringUtils.isBlank(danfeng)&&StringUtils.isBlank(tongyong)) {
				//没选科室
				if (keshihao==0) {
					mubanMap.put("packageCategory", 0);
					List<TbPackageMuban> mubanList = mubanservice.findAllPackageNoPageList(mubanMap);
					List<String> packageId = new ArrayList<String>();
					if (mubanList.size()!=0) {
						for (TbPackageMuban tbPackageMuban : mubanList) {
							packageId.add(tbPackageMuban.getPackageId());
						}
						if (packageId.size()!=0) {
							map.put("packageIdArr", packageId);
						}
						else {
							map.put("packageIdArr", 0);
						}
					}else {
						map.put("packageIdArr", 0);
					}
				}
				//选了科室
				else {
					mubanMap.put("packageCategory", 0);
					mubanMap.put("packageDepartid", keshihao);
					List<TbPackageMuban> mubanList = mubanservice.findAllPackageNoPageList(mubanMap);
					List<String> packageId = new ArrayList<String>();
					if (mubanList.size()!=0) {
						for (TbPackageMuban tbPackageMuban : mubanList) {
							packageId.add(tbPackageMuban.getPackageId());
						}
						if (packageId.size()!=0) {
							map.put("packageIdArr", packageId);
						}
						else {
							map.put("packageIdArr", 0);
						}
					}else {
						map.put("packageIdArr", 0);
					}
				}
			}
			//选中第二个============================================================
			if (StringUtils.isBlank(danfeng)&&!StringUtils.isBlank(tongyong)) {
				//没选科室
				if (keshihao==0) {
					mubanMap.put("packageCategory", 1);
					List<TbPackageMuban> mubanList = mubanservice.findAllPackageNoPageList(mubanMap);
					List<String> packageId = new ArrayList<String>();
					if (mubanList.size()!=0) {
						for (TbPackageMuban tbPackageMuban : mubanList) {
							packageId.add(tbPackageMuban.getPackageId());
						}
						if (packageId.size()!=0) {
							map.put("packageIdArr", packageId);
						}
						else {
							map.put("packageIdArr", 0);
						}
					}else {
						map.put("packageIdArr", 0);
					}
				}
				//选了科室
				else {
					mubanMap.put("packageCategory", 1);
					mubanMap.put("packageDepartid", keshihao);
					List<TbPackageMuban> mubanList = mubanservice.findAllPackageNoPageList(mubanMap);
					List<String> packageId = new ArrayList<String>();
					if (mubanList.size()!=0) {
						for (TbPackageMuban tbPackageMuban : mubanList) {
							packageId.add(tbPackageMuban.getPackageId());
						}
						if (packageId.size()!=0) {
							map.put("packageIdArr", packageId);
						}
						else {
							map.put("packageIdArr", 0);
						}
					}else {
						map.put("packageIdArr", 0);
					}
				}
			}
			map.put("pageNo", pageNo);
			map.put("pageSize", Const.PAGE_SIZE);
			map.put("linchuang", "linchuang");
			map.put("miejunshehetime", "miejunshehetime");
			map.put("miejunHege", 3);
			map.put("miejunZhuangtai", 2);
 		}
		PageInfo<ZsPackageImformation> page = packageInfomationservice.findAllInformation(map);
		if (page.getList().size()!=0) {
			request.setAttribute("shoushubaoList", page.getList());
			request.setAttribute("total", page.getPages());
			List<TbDepartment> alldeparts=departmentservice.findAllDepartment();
			request.setAttribute("alldeparts",alldeparts );
			request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
			return new ModelAndView("/wupinfafang/linchuangkeshibao/linchuangbao_list");
		}else {
			request.setAttribute("shoushubaoList", "");
			request.setAttribute("total", 1);
			List<TbDepartment> alldeparts=departmentservice.findAllDepartment();
			request.setAttribute("alldeparts",alldeparts );
			request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
			return new ModelAndView("/wupinfafang/linchuangkeshibao/linchuangbao_list");
		}
	}
	
	/**
	 * 判断只能发放单封包
	 * 点击批量发放按钮
	 * 单封包只能入库
	 * 通用包只能发放
	 * @param request
	 * @param response
	 */
	//物品发放--临床科室包发放    点击发放
	@RequestMapping("/judgebao.do")
	@ResponseBody
	public Map<Object, Object> judgebao(String fafangren,HttpServletRequest request, HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {
			Date date = new Date();
			String fafangkeshi = request.getParameter("fafangkeshi");
			List<Integer> list = new ArrayList<>();
			List<Integer> list1 = new ArrayList<>();
			String str  = request.getParameter("tiaomas");
			JSONArray jsonArray = JSONObject.parseArray(str);
			String [] baotiaomas = new String[jsonArray.size()];
			for (int i = 0; i < jsonArray.size(); i++) {
				baotiaomas[i] = jsonArray.getString(i);

			}
			TbEmployee tbEmployee = employeeService.selectByEmployeeNo(fafangren);
			fafangren = fafangren.toUpperCase();
			if (tbEmployee!=null) {
				ZsPackageImformation zsPackageImformation=null;
				Integer packagetDepartId = null;
				Integer packagecategory = null;
				for (int i = 0; i < baotiaomas.length; i++) {
					zsPackageImformation = packageInfomationservice.selectByPrimaryKey(baotiaomas[i]);
					//获得包的科室id 和 包的类型(通用包和单封包)
					packagetDepartId = zsPackageImformation.getTbPackageMuban().getPackageDepartid();
					packagecategory = zsPackageImformation.getTbPackageMuban().getPackageCategory();
					//keshi==packagetDepartId
					if (true) {//每一个都相等，包和科室才匹配
						if (!list.contains(packagetDepartId)) {
							list.add(packagetDepartId);
						}
					}else {
						list.add(packagetDepartId);
					}
					if (packagecategory==0) {
						if (!list1.contains(packagecategory)) {
							list1.add(packagecategory);
						}
					}
//					}else {
//						System.out.println("这是通用包，不能批量发放");
//						list1.add(packagecategory);
//					}
				}
				//list.size()==1&&list.get(0)==keshi
				if (true) {
					//list1.size()==1&&list1.get(0)!=1
					if (true) {
						SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
						String proId = sdf.format(date)+ seqService.seqVal("wupinfafangno");

						ZsWupinFafang wupingfafang = new ZsWupinFafang();
						wupingfafang.setFafangLiushui(proId);
                        wupingfafang.setFafangrenTiaoma(fafangren);
						wupingfafang.setFangfangLeixing(0);
						wupingfafang.setFafangRiqi(date);
			            wupingfafang.setFangfaKeshi(Integer.valueOf(fafangkeshi));
						wupingfafang.setRecordTime(date);
						if (baotiaomas.length > 0) {
							List<ZsWupinBaoFafang> wupingbaos = new ArrayList<>();
							List<ZsPackageImformation> packgaesListOne = new ArrayList<>();
							Long[] seqArr = seqService.seqValBatch("baofafangliushui",baotiaomas.length);
							// 发放明细表中插入所有数据
							ZsWupinBaoFafang baofafang = null;
							ZsPackageImformation onePackageImformation = null;
							for (int i = 0; i < baotiaomas.length; i++) {
								baofafang = new ZsWupinBaoFafang();
								baofafang.setRecordTime(date);
								//FIXME 发放时包的明细表中修改数据不成功时，可以申请表中数据已经更改。事务控制失败
								baofafang.setBaoFafangLiushui(sdf.format(date)+ seqArr[i]);
								baofafang.setFafangLiushui(wupingfafang.getFafangLiushui());
								baofafang.setWupinbaoTiaoma(baotiaomas[i]);
								baofafang.setWupinbaoShuliang(1);
								wupingbaos.add(baofafang);
								// baofafangservice.insert(baofafang);
								onePackageImformation = packageInfomationservice.selectByPrimaryKey(baotiaomas[i]);
								onePackageImformation.setFafangLiushui(proId);
								onePackageImformation.setPackageLiuchengZhuangtai(6);
								onePackageImformation.setFafangrenTiaoma(fafangren);
								onePackageImformation.setFafangTime(date);
								onePackageImformation.setPackageLiuchengZhuangtai(6);
								onePackageImformation.setFafangrenName(tbEmployee.getEmployeeName());
								packgaesListOne.add(onePackageImformation);
							}
							wupingfafangservice.insert(wupingfafang);
							//批量插入包发放信息，批量修改packageInfomation表中的发放流水
							baofafangservice.insertPiliang(wupingbaos);
							packageInfomationservice.updateByBatch(packgaesListOne);
						}
						rtnMap.put("status", "true");
						rtnMap.put("msg", "发放成功");
					}else {
						rtnMap.put("status", "false");
						rtnMap.put("msg", "只能发放单封包");
					}
				}else {
					rtnMap.put("status", "false");
					rtnMap.put("msg", "包和科室不匹配");
				}
				
			}else {
				rtnMap.put("status", "false");
				rtnMap.put("msg", "无效发放人条码");
			}
			
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "发放失败");
			e.printStackTrace();
		}
		return rtnMap;
    }
	
	
	/**
	 * 入库的时候不用选择科室
	 * @param request
	 * @param baoruku
	 * @param response
	 * @throws BusinessException
	 */
	//物品发放--临床科室包发放    点击入库
	@RequestMapping("/baoruku.do")
	@ResponseBody
	public Map<Object, Object> baoruku(HttpServletRequest request,String baoruku, HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {
			//包----------------没有入库人   没有入库科室
			String rukuren = request.getParameter("rukuren");
			List<Integer> list1 = new ArrayList<Integer>();
			String str  = request.getParameter("tiaomas");
			JSONArray jsonArray = JSONObject.parseArray(str);
			String [] baotiaomas = new String[jsonArray.size()];
			for (int i = 0; i < jsonArray.size(); i++) {
				baotiaomas[i] = jsonArray.getString(i);

			}
			TbEmployee tbEmployee = employeeService.selectByEmployeeNo(rukuren);
			if (tbEmployee!=null&&tbEmployee.getEmployeeNo().equals(rukuren)) {
				ZsPackageImformation zsPackageImformation=null;
				for (int i = 0; i < baotiaomas.length; i++) {
					zsPackageImformation = packageInfomationservice.selectByPrimaryKey(baotiaomas[i]);
					TbPackageMuban tbPackageMuban = zsPackageImformation.getTbPackageMuban();
					if(tbPackageMuban !=null){
						Integer packagecategory  = tbPackageMuban.getPackageCategory();
						if (packagecategory==1) {
							if (!list1.contains(packagecategory)) {
								list1.add(packagecategory);
							}
						}else {
							list1.add(packagecategory);
						}
					}

				}
				if (list1.size()==1&&list1.get(0)!=0) {
					List<ZsPackageImformation> packageImformations = new ArrayList<ZsPackageImformation>();
					ZsPackageImformation packageImformation=null;
					for (int i = 0; i < baotiaomas.length; i++) {
						packageImformation = packageInfomationservice.selectByPrimaryKey(baotiaomas[i]);
						packageImformation.setPackageTiaoma(baotiaomas[i]);
						packageImformation.setBaoRuku(1);
						packageImformation.setFafangrenTiaoma(rukuren);
//					    packageImformation.setFafangLiushui(fafangLiushui);
//				        packageImformation.setFafangMingxiliushui(fafangMingxiliushui);
						packageImformations.add(packageImformation);
					}
					packageInfomationservice.updateByBatch(packageImformations);
					rtnMap.put("status", "true");
					rtnMap.put("msg", "入库成功");
				}else {
					rtnMap.put("status", "false");
					rtnMap.put("msg", "只能入库通用包");
				}
			}else {
				rtnMap.put("status", "false");
				rtnMap.put("msg", "无效入库人条码");
			}
	    } catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "入库失败");
			e.printStackTrace();
		}
		return rtnMap;
	}

	//物品发放--物品申请信息   点击发放（发放包）
	@RequestMapping("/wupinshenqing.do")
	public ModelAndView wuoinshenqing(String searchTxt,String keshi,String shuliang,String fafangren,
			HttpServletRequest request, HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex") == null?1:Integer.valueOf(request.getParameter("pageIndex"));
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> mubanMap = new HashMap<>();
		String shenqingliushui = request.getParameter("shenqingliushui");
		mubanMap.put("packageCategory", 1);
		Integer keshihao = 0;
		TbDepartment tbDepartment = new TbDepartment();
		fafangren = fafangren.toUpperCase();
		if (!StringUtils.isBlank(keshi)) {
			keshihao = Integer.valueOf(keshi);
			mubanMap.put("packageDepartid", keshihao);
			tbDepartment = tbDepartmentService.selectByPrimaryKey(keshihao);
		}
		List<TbPackageMuban> mubanList = mubanservice.findAllPackageNoPageList(mubanMap);
		List<String> packageId = new ArrayList<>();
		if (!mubanList.isEmpty()) {
			for (TbPackageMuban tbPackageMuban : mubanList) {
				if (tbPackageMuban.getPackageDepartid()!=15) {
					packageId.add(tbPackageMuban.getPackageId());
				}
			}
		}else {
			packageId.add("0");
		}
		if (packageId.size()!=0) {
			map.put("packageIdArr", packageId);
			map.put("pageNo", pageNo);
			map.put("pageSize", Const.PAGE_SIZE);
			map.put("miejunHege", 3);
			map.put("miejunZhuangtai", 2);
			map.put("baoRuku",1);
//			PageInfo<ZsPackageImformation> page = packageInfomationservice.findAllInformation(map);
			List<ZsPackageImformation> list = packageInfomationservice.findAll(map);
			if (list.size()!=0) {
				request.setAttribute("shenqingList", list);
//				request.setAttribute("total", page.getPages());
			}else {
				request.setAttribute("shenqingList", "");
//				request.setAttribute("total", 1);
			}
		}
		request.setAttribute("fafangren", fafangren);
		request.setAttribute("shuliang", shuliang);
		request.setAttribute("keshi", keshihao);
		request.setAttribute("tbDepartment", tbDepartment);
		List<TbDepartment> alldeparts=departmentservice.findAllDepartment();
		request.setAttribute("alldeparts",alldeparts );
		request.setAttribute("shenqingliushui", shenqingliushui);;
		request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
		return new ModelAndView("/wupinfafang/wupinshenqing/shoushubaotiaoma");
	}

	//物品发放--服用无菌包库存
	@RequestMapping("/kucunxinxi.do")
	public ModelAndView kucunxinxi(String id,String searchTxt,
			HttpServletRequest request, HttpServletResponse response)throws BusinessException{
		//从总表查询可以已经入库或还未入库的包
		Integer pageNo = request.getParameter("pageIndex") == null?1:Integer.valueOf(request.getParameter("pageIndex"));
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> groupmap = new HashMap<String, Object>();
		Map<String, Object> mubanMap = new HashMap<String,Object>();
		List<TbPackageMuban> mubanList = mubanservice.findAllPackageNoPageList(mubanMap);
		List<String> packageId = new ArrayList<String>();
		TbPackageMuban packageMuban = new TbPackageMuban();
		if (StringUtils.isNotBlank(id)) {
			packageMuban = packagemubanService.selectByPrimaryKey(id);
			map.put("packageId", id);
		}
		for (TbPackageMuban tbPackageMuban : mubanList) {
			if (tbPackageMuban.getPackageDepartid()!=null) {
				if (tbPackageMuban.getPackageDepartid()!=15) {
					packageId.add(tbPackageMuban.getPackageId());
				}
			}
		}
		if (packageId.size()!=0) {
			map.put("packageIdArr", packageId);
		}
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		map.put("baoRuku", 1);
		map.put("miejunHege", 3);
		groupmap.put("baoRuku", 1);
		groupmap.put("miejunHege", 3);
		groupmap.put("miejunZhuangtai", 2);
		List<KucunNum> kuncunnum  = packageInfomationservice.selectByGroup(groupmap);
//		PageInfo<ZsPackageImformation> page = packageInfomationservice.findAllInformation(map);
		List<ZsPackageImformation> list = packageInfomationservice.findAll(map);
		if (!list.isEmpty()) {
			request.setAttribute("kucunbaoList", list);
		}else {
			request.setAttribute("kucunbaoList", "");
		}
		mubanMap.put("packageCategory", 1);
		List<TbPackageMuban> packageMubans = mubanservice.findAllPackageNoPageList(mubanMap);
		List<TbDepartment> alldeparts=departmentservice.findAllDepartment();
		request.setAttribute("alldeparts",alldeparts );
		request.setAttribute("mubans", packageMubans);
//		request.setAttribute("kucunbaoList", page.getList());
		request.setAttribute("kuncunnum", kuncunnum);
//		request.setAttribute("total", page.getPages());
		request.setAttribute("packageMuban", packageMuban);
		request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
		return new ModelAndView("/wupinfafang/wupinkucunxinxi/shoushubaokucun_list");
	}

	//物品发放--服用无菌包库存  点击查询
	@RequestMapping("/baolxchaxun.do")
	public ModelAndView baolxchaxun(String id,String searchTxt,
			HttpServletRequest request, HttpServletResponse response) throws BusinessException{
//		ZsPackageImformation packageImformation = packageInfomationservice.selectByPrimaryKey(id);
		Map<String, Object> map= new HashMap<String,Object>();
		Integer pageNo = request.getParameter("pageIndex") == null?1:Integer.valueOf(request.getParameter("pageIndex"));
		Map<String, Object> mubanMap = new HashMap<String,Object>();
		Map<String, Object> groupmap = new HashMap<String, Object>();
		groupmap.put("baoRuku", 1);
		groupmap.put("miejunHege", 3);
		groupmap.put("miejunZhuangtai", 2);
		List<KucunNum> kuncunnum  = packageInfomationservice.selectByGroup(groupmap);
		request.setAttribute("kuncunnum", kuncunnum);
		TbPackageMuban packageMuban = packagemubanService.selectByPrimaryKey(id);
		map.put("baoRuku", 1);
		map.put("miejunHege", 3);
		map.put("miejunZhuangtai", 2);
		mubanMap.put("packageCategory", 1);
		List<TbPackageMuban> packageMubans = mubanservice.findAllPackageNoPageList(mubanMap);
		if (StringUtils.isBlank(id)) {
			map.put("pageNo", pageNo);
			map.put("pageSize", Const.PAGE_SIZE);
		}else {
			Integer packageId= Integer.valueOf(id);
			map.put("packageId", packageId);
			map.put("pageNo", pageNo);
			map.put("pageSize", Const.PAGE_SIZE);
		}
		PageInfo<ZsPackageImformation> page = packageInfomationservice.findAllInformation(map);
		List<TbDepartment> alldeparts=departmentservice.findAllDepartment();
		request.setAttribute("alldeparts",alldeparts );
		request.setAttribute("kucunbaoList", page.getList());
		request.setAttribute("total", page.getPages());
		request.setAttribute("mubans", packageMubans);
		request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
		request.setAttribute("packageMuban", packageMuban);
		return new ModelAndView("/wupinfafang/wupinkucunxinxi/shoushubaokucun_list");
	}

	//物品发放--无用五金包库存   点击置换
	@RequestMapping("/zhihuan.do")
	@ResponseBody
	public Map<Object, Object> zhihuan(String keshi,String fafangren,HttpServletRequest request, HttpServletResponse response) throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {
			//不用判断包的数量
			Date date = new Date();
			String str  = request.getParameter("tiaomas");
			JSONArray jsonArray = JSONObject.parseArray(str);
			String [] tiaomas = new String[jsonArray.size()];
			for (int i = 0; i < jsonArray.size(); i++) {
				tiaomas[i] = jsonArray.getString(i);

			}
			Integer keshihao = 0;
			List<Integer> keshiList = new ArrayList<>();
			List<ZsPackageImformation> imformations = new ArrayList<>();
			SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");
			String proId = sdf.format(date)+seqService.seqVal("wupinfafangno");
			List<ZsWupinBaoFafang> wupinBaoFafangs = new ArrayList<>();
	        if (StringUtils.isNotBlank(keshi)) {
				keshihao=Integer.valueOf(keshi);
			}
	        ZsPackageImformation packageImformation = null;
	        for (int i = 0; i < tiaomas.length; i++) {
	        	packageImformation = packageInfomationservice.selectByPrimaryKey(tiaomas[i]);
			    if (packageImformation.getTbPackageMuban().getPackageDepartid()!=keshihao) {
				   if (!keshiList.contains(packageImformation.getTbPackageMuban().getPackageDepartid())) {
					   keshiList.add(packageImformation.getTbPackageMuban().getPackageDepartid());
				   } 
			    }
	        }

			TbEmployee tbEmployee = employeeService.selectByEmployeeNo(fafangren);
			if (tbEmployee!=null&&tbEmployee.getEmployeeNo().equals(fafangren)) {
				ZsWupinFafang wupingfafang=new ZsWupinFafang();
				wupingfafang.setFafangLiushui(proId);
//					int fafangleixin=sq.getShenqingFenlei();
				wupingfafang.setFafangrenTiaoma(fafangren);
				wupingfafang.setFangfaKeshi(keshihao);
				wupingfafang.setFangfangLeixing(0);
				wupingfafang.setFafangRiqi(date);
//					wupingfafang.setShenqingLiushui(shenqingls);
				wupingfafang.setRecordTime(date);
				wupingfafangservice.insert(wupingfafang);
				Long[] seqArr = seqService.seqValBatch("baofafangliushui",tiaomas.length);
				ZsWupinBaoFafang wupinbaofafang = null;
				ZsPackageImformation packageImfo = null;
				for (int i = 0; i < tiaomas.length; i++) {
					wupinbaofafang = new ZsWupinBaoFafang();
					wupinbaofafang.setFafangLiushui(proId);
					wupinbaofafang.setFafangrenTiaoma(fafangren);
					wupinbaofafang.setRecordTime(date);
//						wupinbaofafang.setShenqingLiushui(shenqingls);
					wupinbaofafang.setWupinbaoShuliang(1);
					wupinbaofafang.setBaoFafangLiushui(sdf.format(date)+seqArr[i]);
					packageImfo = packageInfomationservice.selectByPrimaryKey(tiaomas[i]);
					packageImfo.setFafangLiushui(proId);
					packageImfo.setFafangMingxiliushui(sdf.format(date)+ seqArr[i]);
					packageImfo.setBaoRuku(0);
					packageImfo.setFafangTime(date);
					packageImfo.setFafangrenTiaoma(fafangren);
					packageImfo.setFafangrenName(tbEmployee.getEmployeeName());
					imformations.add(packageImfo);
					wupinbaofafang.setWupinbaoTiaoma(tiaomas[i]);
					wupinBaoFafangs.add(wupinbaofafang);
				}
				baofafangservice.insertPiliang(wupinBaoFafangs);
				packageInfomationservice.updateByBatch(imformations);
				rtnMap.put("status", "true");
				rtnMap.put("msg", "发放成功");
			}else {
				rtnMap.put("status", "false");
				rtnMap.put("msg", "无效发放人条码");
			}
//	        if (keshiList.size()!=0) {
//	        	if (keshiList.size()>1||keshiList.get(0)!=keshihao) {
//					System.out.println("有与科室不匹配的包");
//					rtnMap.put("status", "false");
//					rtnMap.put("msg", "有与发放科室不匹配的包");
//				}
//			}else {
//
//
//			}
		}catch(Exception e){
			rtnMap.put("status", "false");
			rtnMap.put("msg", "发放失败");
			e.printStackTrace();
		}
		return rtnMap;
	}

	@RequestMapping("/checkriqi.do")
	@ResponseBody
	public Map checkriqi(HttpServletRequest request, HttpServletResponse response) throws BusinessException, ParseException{
		Date date = new Date();
		System.out.println("当前时间是:---"+new Date().toString());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowtime = sdf.format(date);
		//得到的是当前的时间
		long nowDate = sdf.parse(nowtime).getTime();
		long oneday = 24*60*60*1000;
		List<ZsPackageImformation> guoqibaoList = new ArrayList<ZsPackageImformation>();
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("baoRuku", 1);
		//配包关包时间，开始计算包有效期
		List<ZsPackageImformation> packageImformations = packageInfomationservice.findAll(map);
		//循环     一个包一个包的判断
		for (ZsPackageImformation zsPackageImformation : packageImformations) {
			//得到包的有效期
			Integer youxiaoqi = null;
			ZsDabao dabao = zsPackageImformation.getTbPackageMuban().getZsDabao();
			if (dabao!=null) {
			    youxiaoqi = dabao.getDabaoYouxiaoqiDays();
			}
			//得到包的有效期
			String guanbaotime = sdf.format(zsPackageImformation.getPeibaoGuanbaoTime());
			//包的失效日期
			long shixiaoriqi= sdf.parse(guanbaotime).getTime()+youxiaoqi*oneday+oneday/2;
			//得到当前日期
			long danqian = nowDate;
			//得到失效日期与当前日期的时间差
			long timecha = shixiaoriqi-danqian;
			if (timecha<=2*oneday) {
				if (!guoqibaoList.contains(zsPackageImformation)) {
					guoqibaoList.add(zsPackageImformation);
				}
			}
		}
		if (guoqibaoList.size()!=0) {
			rtnMap.put("guoqibaoList", guoqibaoList);
			rtnMap.put("status", "true");
			rtnMap.put("msg", "有包快要失效了");
		}else {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "无快要失效的包");
		}
		return rtnMap;
	}

	//物品发放--回收包查询   （点击查询）
	@RequestMapping("/huishouList.do")
	public ModelAndView huishouList(String searchTxt,String type, String st, String et, HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<>();
		Map<String,Object> tiaojianMap =new HashMap<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		List<HuishouZhuangtai> huishouZhuangtais = new ArrayList<HuishouZhuangtai>();
        Date startTime = null;
        Date endTime = null;
        
        List<String> tiaomaList = new ArrayList<>();
		if(StringUtils.isNotBlank(searchTxt)){
			tiaojianMap.put("tiaojian", searchTxt);
			List<TbEmployee> employees = employeeService.findEmployee(tiaojianMap);
			List<ZsHuishou> huishous = huishouService.findAllHuishouNoPage(tiaojianMap);
			if (!employees.isEmpty()) {
				for (TbEmployee tbEmployee : employees) {
					if (!tiaomaList.contains(tbEmployee.getEmployeeNo())) {
						tiaomaList.add(tbEmployee.getEmployeeNo());
					}
				}
				map.put("huishourenArr", tiaomaList);
			}
			if (!huishous.isEmpty()) {
				map.put("tiaojian", searchTxt);
			}
			if (employees.isEmpty() && huishous.isEmpty()) {
				tiaomaList.add("0");
				map.put("huishourenArr", tiaomaList);
			}
		}
		try {
			if (StringUtils.isNotBlank(st)) {
				startTime = sdf.parse(st);
				map.put("startTime",startTime);
			}
	        if (StringUtils.isNotBlank(et)) {
	        	endTime = sdf.parse(et);
	        	map.put("endTime", endTime);
			}
	        if (StringUtils.isBlank(st) && StringUtils.isBlank(et)) {
	        	map.put("startTime",ZsMiejunController.lastDayWholePointDate(new Date()));
	        	map.put("endTime", new Date());
			}
        }catch (ParseException e) {
			e.printStackTrace();
		}
		
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<ZsHuishou> page = huishouService.findAllHuiShou(map);
		if (page.getList().size()!=0) {
			HuishouZhuangtai huishouZhuangtai = null;
			for (ZsHuishou zsHuishou : page.getList()) {
				huishouZhuangtai = new HuishouZhuangtai();
				huishouZhuangtai.setHuishou(zsHuishou);
				String huishouliushui = zsHuishou.getHuishouLiushui();
				Map<String, Object> mingxiMap = new HashMap<String,Object>();
				mingxiMap.put("huishouLiushui", huishouliushui);
				List<ZsHuishouMingxi> fafangmingxi = new ArrayList<ZsHuishouMingxi>();
				List<ZsHuishouMingxi> huishouMingxis = huishouMingxiService.findAllHuishouMingxi(mingxiMap,0);
				for (ZsHuishouMingxi zsHuishouMingxi : huishouMingxis) {
					ZsPackageImformation packageImformation = zsHuishouMingxi.getPackageImformation();
					String fafangliushui=null;
					if (packageImformation!=null) {
						fafangliushui = packageImformation.getFafangLiushui();
					}
					if(StringUtils.isNotBlank(fafangliushui)){
						if (!fafangmingxi.contains(zsHuishouMingxi)) {
							fafangmingxi.add(zsHuishouMingxi);
						}
					}
				}
				Integer i = 0;
				if (huishouMingxis.size()!=fafangmingxi.size()) {
					i=1;
					huishouZhuangtai.setI(i);
				}else {
	                i=2;
	                huishouZhuangtai.setI(i);
				}
				huishouZhuangtais.add(huishouZhuangtai);
			}
			request.setAttribute("huishou", huishouZhuangtais);
			request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
			request.setAttribute("total", page.getPages());
			request.setAttribute("empmap",StartUpOnLoadData.emMap);
			request.setAttribute("st", startTime);
			request.setAttribute("et", endTime);
			return new ModelAndView("/wupinfafang/huishouchaxun/huishou_list");
		}else {
			request.setAttribute("total", 1);
			request.setAttribute("huishou", "");
			request.setAttribute("st", startTime);
			request.setAttribute("et", endTime);
			request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
			return new ModelAndView("/wupinfafang/huishouchaxun/huishou_list");
		}
	}
	
	@RequestMapping("/fafangnum.do")
	public ModelAndView fafangnum(String keshi,String searchTxt, HttpServletRequest request,
			HttpServletResponse response) throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<String,Object>();
		Integer keshihao = null;
		TbDepartment department = null;
		if (StringUtils.isNotBlank(keshi)) {
			keshihao=Integer.valueOf(keshi);
			map.put("packageKeshi", keshihao);
			department = tbDepartmentService.selectByPrimaryKey(keshihao);
		}
		map.put("fafangLiushui","fafangliushui");
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<KucunNum> page = packageInfomationservice.selectKeshibaoNum(map);
		if (page.getList().isEmpty()) {
			request.setAttribute("keshibaojilu", "");
			request.setAttribute("total", 1);
		}else {
			request.setAttribute("keshibaojilu", page.getList());
			request.setAttribute("total", 1);
		}
		request.setAttribute("dep", department);
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		List<TbDepartment> alldeparts=departmentservice.findAllDepartment();
		request.setAttribute("alldeparts",alldeparts );
		return new ModelAndView("wupinfafang/wupinfangfajilu/keshibaojilu");
	}

	//物品发放--厂家包发放
	@RequestMapping("/changjiabaoList.do")
	public ModelAndView changjiabaoList(HttpServletRequest request,
			HttpServletResponse response) throws BusinessException{
		List<TbDepartment> alldeparts=departmentservice.findAllDepartment();
		request.setAttribute("alldeparts",alldeparts );
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> mubanmap = new HashMap<String,Object>();
		List<String> packageIdList = new ArrayList<String>();
		mubanmap.put("packageDepartid", null);
		List<TbPackageMuban> mubanList = mubanservice.findAllPackageNoPageList(mubanmap);
		for (TbPackageMuban tbPackageMuban : mubanList) {
			if (!packageIdList.contains(tbPackageMuban.getPackageId())) {
				packageIdList.add(tbPackageMuban.getPackageId());
			}
		}
		if (packageIdList.size()!=0) {
			map.put("packageIdArr", packageIdList);
			map.put("miejunHege", 3);
			map.put("miejunZhuangtai", 2);
//			map.put("shoushu", "shoushu");
			map.put("miejunshehetime", "miejunshehetime");
			map.put("changjia", "changjia");
			List<ZsPackageImformation> page = packageInfomationservice.findAll(map);
			request.setAttribute("shoushubaoList", page);
		}else {
			request.setAttribute("shoushubaoList", "");
		}
		return new ModelAndView("wupinfafang/changjiabaofafang/changjiabao_fafang");
	}

	//物品发放-厂家包发放   点击发放
	@RequestMapping("/changjiabaofafang.do")
	@ResponseBody
	public Map<Object, Object> changjiabaofafang(String keshi,String fafangren,HttpServletRequest request,HttpServletResponse response) throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {
			//此处发放分为  手术室发放   和   厂家发放(根据流程状态和包模板中科室ID判断)
			Date date = new Date();
			String [] baotiaomas = request.getParameter("baotiaomas").split(",");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String proId = sdf.format(date)+ seqService.seqVal("wupinfafangno");
			TbEmployee tbEmployee = employeeService.selectByEmployeeNo(fafangren);
			List<String> tiaomaList = new ArrayList<>();
			if (tbEmployee!=null) {
				if (baotiaomas.length > 0) {
					ZsWupinFafang wupingfafang = new ZsWupinFafang();
					wupingfafang.setFafangLiushui(proId);
					wupingfafang.setFangfangLeixing(0);
					wupingfafang.setFafangRiqi(date);
					wupingfafang.setRecordTime(date);
                    wupingfafang.setFafangrenTiaoma(fafangren.toUpperCase());
					List<ZsWupinBaoFafang> wupingbaos = new ArrayList<>();
					List<ZsPackageImformation> packgaesListOne = new ArrayList<>();
					Long[] seqArr = seqService.seqValBatch("baofafangliushui",baotiaomas.length);
					ZsWupinBaoFafang baofafang = null;
					ZsPackageImformation onePackageImformation = null;
					ZsPackageImformation packageImformation = null;
					//判断勾选的所有的包的发放对象是否都一样
					if (StringUtils.isNotBlank(keshi)) {
						System.out.println("发放给手术室");
						wupingfafang.setFangfaKeshi(15);
						for (int i = 0; i < baotiaomas.length; i++) {
							packageImformation = packageInfomationservice.selectByPrimaryKey(baotiaomas[i]);
							if (packageImformation.getPackageLiuchengZhuangtai()>5) {
								//是发放给科室的包
								tiaomaList.add(baotiaomas[i]);
							}
							if (tiaomaList.isEmpty()) {
								baofafang = new ZsWupinBaoFafang();
								baofafang.setRecordTime(date);
								baofafang.setBaoFafangLiushui(sdf.format(date)+ seqArr[i]);
								baofafang.setFafangLiushui(wupingfafang.getFafangLiushui());
								baofafang.setWupinbaoTiaoma(baotiaomas[i]);
								baofafang.setWupinbaoShuliang(1);
								baofafang.setFafangrenTiaoma(fafangren.toUpperCase());
								wupingbaos.add(baofafang);
								// baofafangservice.insert(baofafang);
								onePackageImformation = packageInfomationservice.selectByPrimaryKey(baotiaomas[i]);
								onePackageImformation.setFafangLiushui(proId);
								onePackageImformation.setPackageLiuchengZhuangtai(6);
								onePackageImformation.setFafangrenTiaoma(fafangren.toUpperCase());
								onePackageImformation.setFafangTime(date);
								packgaesListOne.add(onePackageImformation);
							}
						}
					}else {
						wupingfafang.setFangfaKeshi(null);
						for (int i = 0; i < baotiaomas.length; i++) {
							packageImformation = packageInfomationservice.selectByPrimaryKey(baotiaomas[i]);
							if (packageImformation.getPackageLiuchengZhuangtai()<11&& packageImformation.getPeibaoHege()!=1) {
								//是发放给科室的包
								tiaomaList.add(baotiaomas[i]);
							}
							if (packageImformation.getPeibaoGuanbaoTime().getTime()<=packageImformation.getMiejunShenheTime().getTime()) {
								System.out.println("清洗时间比灭菌时间晚");
								tiaomaList.add(baotiaomas[i]);
							}
							if (tiaomaList.isEmpty()) {
								baofafang = new ZsWupinBaoFafang();
								baofafang.setRecordTime(date);
								//FIXME 发放时包的明细表中修改数据不成功时，可以申请表中数据已经更改。事务控制失败
								baofafang.setBaoFafangLiushui(sdf.format(date)+ seqArr[i]);
								baofafang.setFafangLiushui(wupingfafang.getFafangLiushui());
								baofafang.setWupinbaoTiaoma(baotiaomas[i]);
								baofafang.setWupinbaoShuliang(1);
								baofafang.setFafangrenTiaoma(fafangren.toUpperCase());
								wupingbaos.add(baofafang);
								// baofafangservice.insert(baofafang);
								onePackageImformation = packageInfomationservice.selectByPrimaryKey(baotiaomas[i]);
								onePackageImformation.setFafangLiushui(proId);
								onePackageImformation.setPackageLiuchengZhuangtai(11);
								onePackageImformation.setFafangrenTiaoma(fafangren.toUpperCase());
								onePackageImformation.setFafangTime(date);
								packgaesListOne.add(onePackageImformation);
							}
						}
					}
					if (tiaomaList.isEmpty()) {
						wupingfafangservice.insert(wupingfafang);
						baofafangservice.insertPiliang(wupingbaos);
						packageInfomationservice.updateByBatch(packgaesListOne);
						rtnMap.put("status", "true");
						rtnMap.put("msg", "发放成功");
					}else {
						rtnMap.put("status", "false");
						rtnMap.put("msg", "操作错误");
					}
				}
			}else {
				rtnMap.put("status", "false");
				rtnMap.put("msg", "无效发放人条码");
			}
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "发放失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping("/shoushushi.do")
	@ResponseBody
	public Map<Object, Object> shoushushi(HttpServletRequest request,HttpServletResponse response) throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object, Object>();
		Map<String, Object> map = new HashMap<String,Object>();
		List<String> packageIdArr = new ArrayList<String>();
		try {
			map.put("packageDepartid",15);
			List<TbPackageMuban> packageMubans = packagemubanService.findAllPackageNoPageList(map);
			for (TbPackageMuban tbPackageMuban : packageMubans) {
				if (!packageIdArr.contains(tbPackageMuban.getPackageId())) {
					packageIdArr.add(tbPackageMuban.getPackageId());
				}
			}
			if (!packageIdArr.isEmpty()) {
				map.put("packageIdArr", packageIdArr);
			}else {
				packageIdArr.add("0");
				map.put("packageIdArr", packageIdArr);
			}
			List<ZsPackageImformation> packageImformations = packageInfomationservice.findAll(map);
			rtnMap.put("status", "true");
			rtnMap.put("msg", "查询成功");
			rtnMap.put("package",packageImformations);
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
}